//
//  FSEmoticonViewModel.m
//  BiaoQing
//
//  Created by 张帅 on 15/10/12.
//  Copyright © 2015年 ZhangShuai. All rights reserved.
//

#import "FSEmoticonViewModel.h"
#import "FSEmoticonPackageModel.h"
#import "FSEmoticonModel.h"
#import <UIKit/UIKit.h>



@implementation FSEmoticonViewModel


+(instancetype)shareEmoticonViewModel{
    
    static FSEmoticonViewModel *instance  =nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance =[[self alloc] init];
    });
    return instance;
}


-(instancetype)init{
    if (self==[super init]) {
        
        [self loadData];
    
    }

    return self;
}


//
//-(NSAttributedString *)emoticonTextStr:(NSString *)str font:(UIFont *)font{
//    
//  NSString *pattern  = @"\\[.*?\\]";
// 
//    NSRegularExpression *regex  = [NSRegularExpression  regularExpressionWithPattern:pattern options:0 error:nil];
//      // matchesInString 查找 pattern 所有的匹配项
//    
//    
//    NSArray *results = [regex matchesInString:str options:0 range:NSMakeRange(0, str.length)];
//    
//    // 获得数组数量
//    
//    
//    NSInteger count = results.count;
//    // 准备属性字符串
//    
//    NSMutableAttributedString *strM = [[NSMutableAttributedString alloc] initWithString:str];
//    
//    // 根据数量`倒序`遍历数组内容
//
//    while (count>0) {
//         NSRange range= [results[--count] rangeAtIndex:0];
//        // 根据 range 获取到对应的 chs 字符串
//    
//        NSString *chs  = [str substringWithRange:range];
//         // 根据 chs 获得对应的 emoticon 对象
//        
////        FSEmoticonModel *emoticon = [self ];
//   
//    }
//    
//    
//    
//    
// }

//
//-(FSEmoticonModel*)emoticon:(NSString*)str{
//
//
//    FSEmoticonModel *emoticon =nil;
//    
//    for (FSEmoticonPackageModel *p  in  self.emoticonPackages) {
//    
////        emoticon = [p.emoticons  fileManager:<#(nonnull NSFileManager *)#> shouldProceedAfterError:nil ];
//        
//    }
//    
//
//
//}
//


-(void)favorite:(NSIndexPath *)indexPath{
    // 0. 如果是第0个分组，不参与排
    if (indexPath.section==0) {
        return;
    }
    
    if ([[self emoticon:indexPath] isDeleteButton]) {
        
        return;
    }
    
//    if ([[self.emoticonPackages[indexPath.section] emoticons][indexPath.item] isDeleteButton]) {
//        return;
//    }

    
 
   

    // 1. 获取表情符号
  FSEmoticonModel *emotion = [ self emoticon:indexPath];

    emotion.times++;
    // 2. 将表情符号添加到第0组的首位
    // 判断是否已经存在表情
    if (![[self.emoticonPackages[0] emoticons]containsObject:emotion]) {
        
      
        
        [[self.emoticonPackages[0]emoticons]insertObject:emotion atIndex:0];
        
        
    }
    
    // 3. 对数组进行排序 直接排序当前数组 sortInPlace
    // Swift中，对尾随闭包，同时有返回值的又一个简单的写法
    //        packages[0].emoticons.sortInPlace { (obj1, obj2) -> Bool in
    //            return obj1.times > obj2.times
    //        }
    // $0 对应第一个参数，$1对应第二个参数，依次类推，return 可以省略
    
    [[self.emoticonPackages[0]emoticons] sortWithOptions:NSSortStable usingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
      
        return [obj1 times]>[ obj2 times];
    }];
    
     // 4. 删除多余的表情 － 倒数第二个
    
    if ([self.emoticonPackages[0] emoticons].count>21) {
        
        [[self.emoticonPackages[0] emoticons]removeObjectAtIndex:19];
    }
    
    NSLog(@"emoticons %@",[self.emoticonPackages[0] emoticons]);
    
    NSLog(@" emoticons].count---%ld",[self.emoticonPackages[0] emoticons].count);

    
    
}


-(FSEmoticonModel *)emoticon:(NSIndexPath *)indexpath{
    
 return [self.emoticonPackages[indexpath.section] emoticons][indexpath.item];

}



-(void)loadData{

    NSString *path =[[NSBundle mainBundle] pathForResource:@"emoticons.plist" ofType:nil inDirectory:@"Emoticons.bundle"];
    
    NSDictionary *rootDict  =[ NSDictionary dictionaryWithContentsOfFile:path];
    NSArray *rootArray =rootDict[@"packages"];
    
    NSMutableArray *packagePathArray =[ NSMutableArray array];
    
    NSMutableArray *packageModelArray  =[NSMutableArray array];
    
    for (NSDictionary *dict in rootArray) {
        
        NSString *path =[[NSBundle mainBundle] pathForResource:@"info.plist" ofType:nil inDirectory:[NSString stringWithFormat:@"Emoticons.bundle/%@",dict[@"id"]]];
        [packagePathArray addObject:path];
    
    }
    
    for (NSString *path in packagePathArray) {
        
        NSDictionary *packageDict  =[NSDictionary dictionaryWithContentsOfFile:path];
        
        
        FSEmoticonPackageModel *model =[FSEmoticonPackageModel emoticonPackageModelWithDict:packageDict];
        
        [packageModelArray addObject:model];
        
    }
    
    
    FSEmoticonPackageModel *pModel = [FSEmoticonPackageModel emoticonPackageModelWithDict:nil];
    
    pModel.group_name_cn = @"最近";
    
    [packageModelArray insertObject:pModel atIndex:0];
    
    self.emoticonPackages = packageModelArray.mutableCopy;
    
    NSLog(@"%@",self.emoticonPackages);
    
  
}
-(NSMutableArray *)emoticonPackages{
    if (_emoticonPackages==nil) {
      
        _emoticonPackages=[ NSMutableArray array];
    }
    
    return _emoticonPackages;

}


@end
